package cn.starrysky.hrm.service.impl;

import cn.starrysky.hrm.domain.VipBase;
import cn.starrysky.hrm.domain.VipUser;
import cn.starrysky.hrm.dto.RegisterDto;
import cn.starrysky.hrm.exception.GlobalException;
import cn.starrysky.hrm.mapper.VipBaseMapper;
import cn.starrysky.hrm.mapper.VipUserMapper;
import cn.starrysky.hrm.service.IVipUserService;
import cn.starrysky.hrm.util.StrUtils;
import cn.starrysky.hrm.util.encrypt.MD5;
import cn.starrysky.hrm.utils.ValidUtils;
import cn.starrysky.hrm.vo.SendSmsCodeVo;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author starrysky
 * @since 2021-03-11
 */
@Service
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements IVipUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private VipUserMapper vipUserMapper;
    @Autowired
    private VipBaseMapper vipBaseMapper;

    /**
     * 用户注册
     * @param registerDto
     */
    @Override
    public void register(RegisterDto registerDto) {
        //验证
        String mobile = registerDto.getMobile();
        String password = registerDto.getPassword();
        String smsCode = registerDto.getSmsCode();
        ValidUtils.assertNotNull(mobile,"手机号不能为空" );
        ValidUtils.assertNotNull(password, "密码不能为空");
        ValidUtils.assertNotNull(smsCode, "验证码不能为空");

        //验证码是否正确
        String smsCodeKey="sms:"+mobile;
        SendSmsCodeVo sendSmsCodeVo =(SendSmsCodeVo) redisTemplate.opsForValue().get(smsCodeKey);
        ValidUtils.assertNotNull(sendSmsCodeVo.getSmsCode(), "验证码过期，请重新获取");
        ValidUtils.assertNotSame(sendSmsCodeVo.getSmsCode(), smsCode, "验证码不正确");

        //判断是否一斤注册
        VipUser vipUser= vipUserMapper.selectByMobile(mobile);
        if (vipUser!=null){
           throw  new GlobalException("该手机号已被注册");
        }

        //保存注册信息
        VipUser registerVipUser = new VipUser();
        //电话
        registerVipUser.setPhone(mobile);
        //创建时间
        registerVipUser.setCreateTime(new Date().getTime());
        //加盐
        String salt = StrUtils.getComplexRandomString(10);
        registerVipUser.setSalt(salt);
        //密码
        String md5Pwd = MD5.getMD5(password + salt);
        //
        registerVipUser.setPassword(md5Pwd);
        //给位状态赋值  绑定手机
        //registerVipUser.addBitState(VipUser.BIT_STATE_PHONE);
        registerVipUser.setBitState(VipUser.BIT_STATE_PHONE);
        //保存vipUser
        vipUserMapper.insert(registerVipUser);

        //保存base
        //4.保存base基本信息表
        VipBase vipBase = new VipBase();
        vipBase.setSsoId(registerVipUser.getId());
        vipBase.setCreateTime(new Date().getTime());
        vipBase.setRegChannel(1);   //1.pc断
        vipBase.setRegTime(new Date().getTime());
        vipBaseMapper.insert(vipBase);
    }
}
